﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data;
using DAO;

namespace BUS
{
    public class Tho_BUS
    {

        private static Tho_BUS m_instance = null;

        public static Tho_BUS Instance
        {
            get 
            { 
                if (m_instance==null)
                {
                    m_instance = new Tho_BUS();
                }
                return Tho_BUS.m_instance; 
            }
        }

        /// <summary>
        ///   Lay thong tin Tho theo id
        /// </summary>
        /// 
        public Tho_DTO LayThongTinTho(string id)
        {
            DataTable dt = Tho_DAO.Instance.LayTho(id);
            if (dt == null)
            {
                return null;
            }

            DataRowCollection list = dt.Rows;
            if (list.Count == 0)
            {
                return null;
            }
            DataRow dr  =list[0];
            string _id = (string)dr["MaTho"];
            string _name = (string)dr["TenTho"];
            string _address = (string)dr["DiaChi"];

            return new Tho_DTO(_id, _name, _address);
        }
        
        /// <summary>
        ///   Lay toan bo ma tho
        ///   
        /// </summary>
        /// 
        public List<string> LayToanBoMaTho()
        {
            List<string> result = new List<string>();
            DataTable dt = Tho_DAO.Instance.LayToanBoMaTho();
            DataRowCollection list = dt.Rows;
            if (list == null)
            {
                return null;
            }

            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = list[i];
                string _id = (string)dr["MaTho"];
                result.Add(_id);
            }
            return result;
        }

        /// <summary>
        ///   lay toan bo thong tin tho
        /// </summary>
        /// 
        public List<Tho_DTO> LayToanBoTho()
        {
            DataTable dt = Tho_DAO.Instance.LayToanBoTho();
            if (dt == null)
            {
                return null;
            }
            List<Tho_DTO> result = new List<Tho_DTO>();
            DataRowCollection list = dt.Rows;
            if (list.Count==0)
            {
                return result;
            }

            for (int i = 0; i < list.Count;i++ )
            {
                DataRow dr = list[i];
                string _id = (string)dr["MaTho"];
                string _name = (string)dr["TenTho"];
                string _address = (string)dr["DiaChi"];
                result.Add(new Tho_DTO(_id, _name, _address));

            }
            return result; 
        }
        /// <summary>
        ///   tim kiem thong tin tho
        /// </summary>
        /// 
        public List<Tho_DTO> TimKiemTho(Tho_DTO _tho)
        {
            DataTable dt = Tho_DAO.Instance.TimKiemTho(_tho.MaTho, _tho.TenTho, _tho.DiaChi);
            List<Tho_DTO> result = new List<Tho_DTO>();
            DataRowCollection list = dt.Rows;
            if (list.Count == 0 )
            {
                return result;
            }
            for (int i = 0; i < list.Count;i++ )
            {
                DataRow dr = list[i];
                string _id = (string)dr["MaTho"];
                string _name = (string)dr["TenTho"];
                string _address = (string)dr["DiaChi"];
                result.Add(new Tho_DTO(_id, _name, _address));
            }
            return result;
        }
 

        /// <summary>
        ///   sua thong tin tho
        /// </summary>
        /// 
        public bool SuaThongTinTho(Tho_DTO _tho)
        {
            return Tho_DAO.Instance.CapNhatThongTinTho(_tho.MaTho, _tho.TenTho, _tho.DiaChi);
        }

        /// <summary>
        ///   xoa thong tin tho
        /// </summary>
        /// 
        public bool XoaThongTinTho(Tho_DTO _tho)
        {
            return Tho_DAO.Instance.XoaThongTinTho(_tho.MaTho);
        }

        /// <summary>
        ///   them tho moi
        /// </summary>
        /// 
        public bool ThemThoMoi(Tho_DTO _tho)
        {
            return Tho_DAO.Instance.ThemThoMoi(_tho.TenTho, _tho.DiaChi);
        }
    }
}
